package com.cilico.view;

import com.cilico.components.Component;
import com.cilico.components.CssStyle;
import com.cilico.utils.Utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

/**
 * 
 * @author lv.xiangan
 *
 */
public class LPLinearLayoutView extends LinearLayout implements Component {
	public int scrollY_; // 目前view的锚点。
	int scrollToY_; // 每次拖动的距离
	public int sWidth_;
	public int sHeight_;
	int tableHeight_;
	private ShapeDrawable drawable_;
	private Bitmap backgroundImg_;
	private Paint paint_;

	public LPLinearLayoutView(Context context, int tableH) {
		super(context);
		paint_ = new Paint();
		tableHeight_ = tableH;
	}

	public boolean onTouchEvent(MotionEvent event) {
		int touchEvent = event.getAction();
		int moveTo = 0;
		switch (touchEvent) {
		case MotionEvent.ACTION_MOVE:
			if (scrollToY_ > 0) {// 向上拖动
				if (sHeight_ + scrollY_ < tableHeight_) {
					moveTo = sHeight_ + scrollY_ + scrollToY_ < tableHeight_ ? scrollY_ + scrollToY_ : tableHeight_ - sHeight_;
					scrollTo(0, moveTo);
				}
			} else {// 向下拖动
				if ((moveTo = scrollY_ + scrollToY_) >= 0) {
					scrollTo(0, moveTo);
				}
			}
			break;
		default:
			super.onTouchEvent(event);
		}
		return true;
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int scrolly = getScrollY();
		if (drawable_ != null) {
			drawable_.setBounds(0, scrolly, sWidth_, sHeight_ + scrolly);
			drawable_.draw(canvas);
		}
		if (backgroundImg_ != null) {
			canvas.drawBitmap(backgroundImg_, 0, 0, paint_);
		}
	}

	public void cleanText() {
		// TODO Auto-generated method stub

	}

	public String getContentText() {
		// TODO Auto-generated method stub
		return null;
	}

	public CssStyle getCssStyle() {
		// TODO Auto-generated method stub
		return null;
	}

	public int getCmpHeight() {
		// TODO Auto-generated method stub
		return sHeight_;
	}

	public View getCmpView() {
		// TODO Auto-generated method stub
		return null;
	}

	public int getCmpWidth() {
		// TODO Auto-generated method stub
		return sWidth_;
	}

	public boolean isInTable() {
		// TODO Auto-generated method stub
		return false;
	}

	public void mould() {
		// TODO Auto-generated method stub

	}

	public void setContentText(String text) {
		// TODO Auto-generated method stub

	}

	public void setCssStyle(CssStyle style) {
		// TODO Auto-generated method stub

	}

	public void setInTable(boolean inTable) {
		// TODO Auto-generated method stub

	}

	public void setCmpHeidht(int height) {
		// TODO Auto-generated method stub

	}

	public void setCmpWidth(int width) {
		// TODO Auto-generated method stub

	}

	public void shrinkWidth() {
		// TODO Auto-generated method stub

	}

	public void setShape(ShapeDrawable drawable) {
		drawable_ = drawable;

	}

	public void setBackgroundImg(Bitmap backgroundImg) {
		backgroundImg_ = backgroundImg;
	}

	public void releaseResource(View view) {
		if (null != backgroundImg_ && !backgroundImg_.isRecycled()) {
			backgroundImg_.recycle();
		}
		backgroundImg_ = null;
		Utils.releaseResource(view);
	}

	public void setForm(FormLayout formLayout) {
		// TODO Auto-generated method stub
	}

	public FormLayout getForm() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEncrypt(String encrypt) {
		// TODO Auto-generated method stub
	}
}
